package ycl.security.handler;

import com.alibaba.fastjson.JSONObject;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Service;
import ycl.common.ApiResult;
import ycl.common.enums.ApiResultEnum;
import ycl.common.utils.ServletUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 登录未授权
 *
 * @author ycl
 * @date 2022-03-10 13:16:22
 */
@Service("authAccessDeniedHandler")
public class AuthAccessDeniedHandler implements AccessDeniedHandler {
	@Override
	public void handle(
			HttpServletRequest request,
			HttpServletResponse response,
			AccessDeniedException e) throws IOException {
		ServletUtil.setResultResponse(response);
		ApiResult<Object> result = ApiResult.fail(ApiResultEnum.SC_PAYMENT_REQUIRED);
		response.getWriter().write(JSONObject.toJSONString(result));
		response.getWriter().flush();
	}
}
